package com.ruima.study.arithmetic.linkednode;

import java.util.LinkedList;
import java.util.List;

public class Calculator {
    public static void main(String[] args) {

        Calculator demo = new Calculator();
//      cal("1+2*3-4/2+1");
        int calculate = demo.calculate("2*3+4");
        System.out.println(calculate);
    }

    public int calculate(String s) {
        String numStr = s.replaceAll("\\s+", "");
        List<Integer> numStack = new LinkedList<>();
        List<Character> semStack = new LinkedList<>();

        boolean addTag = false;
        int lastMatch = -1;
        for (int i = 0; i < numStr.length(); i++) {
            char c = numStr.charAt(i);
            if (c >= '0' && c <= '9') {
                StringBuilder builder = new StringBuilder();
                for (int j = lastMatch + 1; j <= i; j++) {
                    builder.append(numStr.charAt(j));
                }
                if (i > 0 && numStr.charAt(i - 1) >= '0' && numStr.charAt(i - 1) <= '9') {
                    numStack.removeLast();
                }
                numStack.addLast(Integer.parseInt(builder.toString()));
            } else {
                addTag = true;
                semStack.addLast(c);
                lastMatch = i;
            }
        }

        if (!addTag) {
            return Integer.parseInt(numStr);
        }


        List<Integer> numStack2 = new LinkedList<>();
        List<Character> semStack2 = new LinkedList<>();
        boolean tag = false;
        for (int i = 0; i < semStack.size(); i++) {
            Character c = semStack.get(i);
            if (c == '/' || c == '*'){
                Integer num = numStack.get(i);
                Integer num2 = numStack.get(i+1);
                Integer result = calNum(num, num2, c);
                numStack2.addLast(result);
                tag = true;
            }else {
                if (tag){
                    numStack2.addLast(numStack.get(i + 1));
                }else {
                    numStack2.addLast(numStack.get(i));
                }
                semStack2.addLast(c);
            }
        }

        /*if (!tag){
            numStack2.addLast(numStack.get(numStack.size() -1));
        }*/

        while (!semStack2.isEmpty()) {
            Integer num1 = numStack2.removeFirst();
            Integer num2 = numStack2.removeFirst();
            char c = semStack2.removeFirst();
            numStack2.addFirst(calNum(num1, num2, c));
        }


        StringBuilder sb = new StringBuilder();
        while (!numStack2.isEmpty()) {
            sb.append(numStack2.getFirst());
            numStack2.removeLast();
        }
        return Integer.parseInt(sb.toString());
    }


    public Integer calNum(Integer num1, Integer num2, char c) {
        if (c == '*') {
            return num1 * num2;
        } else if (c == '/') {
            return num1 / num2;
        } else if (c == '+') {
            return num1 + num2;
        } else if (c == '-') {
            return num1 - num2;
        }
        return null;
    }
}
